package com.atguigu.school.controller;

import com.atguigu.school.commons.Page;
import com.atguigu.school.commons.Result;
import com.atguigu.school.pojo.Grade;
import com.atguigu.school.service.GradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    /**
     * 处理年级相关业务
     */
    @Autowired
    private GradeService gradeService;

    @PostMapping("/saveOrUpdateGrade")
    @ApiOperation(value = "新增或更新年级", notes = "如果是修改，请携带年级 id ，如果是新增，则不要年级 id")
    public Result saveOrUpdateGrade(@RequestBody Grade grade) {
        this.gradeService.saveOrUpdateGrade(grade);
        return Result.ok();
    }

    @DeleteMapping("/deleteGrade")
    @ApiOperation(value = "批量删除年级")
    @ApiImplicitParam(name = "ids", value = "预删除年级的 id 列表")
    public Result deleteGrade(@RequestBody List<Integer> ids) {
        this.gradeService.deleteGradesByIds(ids);
        return Result.ok();
    }

    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    @ApiOperation(value = "按名称分页查询年级")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页，从 1 开始"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度", required = false),
            @ApiImplicitParam(name = "gradeName", value = "年级名称，可以不完整")
    })
    public Result getGrades(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize, String gradeName) {
        Page<Grade> page = this.gradeService.selectGradesForPage(pageNo, pageSize, gradeName);
        return Result.ok(page);
    }

    @GetMapping("/getGrades")
    @ApiOperation(value = "查询所有年级")
    public Result getGrades() {
        return Result.ok(this.gradeService.selectGrades());
    }

}
